<template>
  <div>
    <div style="margin-bottom: 5px">
      <el-input v-model="cname"  placeholder="请输入社团名" suffix-icon="el-icon-search" style="width: 300px"
                @keyup.enter.native="loadPost"></el-input>
      <el-button style="margin-left: 5px" type="primary" icon="el-icon-search" @click="loadPost">查询</el-button>
      <el-button style="margin-left: 5px" type="success" icon='el-icon-refresh-left' @click="reset">重置</el-button>
      <el-button style="margin-left: 5px" type="warning" icon='el-icon-circle-plus-outline' @click="add" >新增</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%"
              :header-cell-style="{background: '#f3f6fd', color: '#211e1e'}"

    >
      <el-table-column prop="cid" label="ID" min-width="40%" align="center">
      </el-table-column>
      <el-table-column prop="cname" label="社团名" min-width="100%" align="center">
      </el-table-column>
      <el-table-column prop="cinfo" label="社团信息" min-width="200%" align="center">
      </el-table-column>
      <el-table-column prop="operate" label="操作" min-width="80%" align="center">
        <template v-slot="scope">
          <el-button type="text" icon="el-icon-edit" size="medium" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？"
              @confirm="del(scope.row.cid)"
              style="margin-left: 5px"
          >
            <el-button slot="reference" type="text" icon="el-icon-delete" size="medium" >删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="提示"
        :visible.sync="centerDialogVisible"
        width="30%"
        center>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="社团名" prop="cname">
          <el-col :span="20">
            <el-input v-model="form.cname" ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="介绍" prop="cinfo" >
          <el-col :span="20">
            <el-input v-model="form.cinfo" type="textarea"></el-input>
          </el-col>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" >
    <el-button @click="centerDialogVisible=false">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "CommunityManage",
  components: {
  },

  data(){
    return {
      disabled_index:false,
      tableData: [],
      pageSize:10,
      pageNum:1,
      total:0,
      cname:'',
      centerDialogVisible:false,
      form:{
        cid:'',
        cname:'',
        cinfo:''
      },
      rules:{
        cname: [
          { required: true, message: '请输入社团名字', trigger: 'blur' },
        ],
        cinfo: [
          { required: true, message: '请备注', trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    // this.loadGet();
    this.loadPost();
  },
  methods:{
    resetForm(){
      this.$refs.form.resetFields();
    },
    mod(row){
      console.log(row)
      this.centerDialogVisible=true
      this.$nextTick(()=>{
        this.form.cid = row.cid
        this.form.cname = row.cname
        this.form.cinfo = row.cinfo
        this.disabled_index = true
      })

    },
    del(id){
      console.log(id)
      this.$axios.get(this.$httpUrl+'/community/delete?cId='+id).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible=false
          this.loadPost()
        }else{
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    loadGet(){
      this.$axios.get(this.$httpUrl+'/community/list').then(res=>res.data).then(res=>{
        console.log(res)
      })
    },
    loadPost(){
      this.$axios.post(this.$httpUrl+'/community/listPage',{
        pageSize:this.pageSize,
        pageNum:this.pageNum,
        params:{
          cname:this.cname
        }
      }).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.tableData=res.data
          this.total=res.total
        }else{
          alert("获取数据失败")
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageNum=1
      this.pageSize=val
      this.loadPost()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum=val
      this.loadPost()
    },
    reset(){
      this.cname=''
      this.loadPost()
    },
    doMod(){
      this.$axios.post(this.$httpUrl+'/community/update',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible=false
          this.loadPost()
        }else{
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    dosave(){
      this.$axios.post(this.$httpUrl+'/community/add',this.form).then(res=>res.data).then(res=>{
        console.log(res)
        if (res.code==200){
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          });
          this.centerDialogVisible=false
          this.loadPost()
        }else{
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          });
        }
      })
    },
    save(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.cid==''){
            console.log("新增")
            this.dosave();
          }else{
            console.log("修改")
            this.doMod();
            this.form.cid=''
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    add(){

      this.centerDialogVisible=true
      this.disabled_index = false
      this.$nextTick(()=>{
        this.resetForm()
      })
    }
  }
}
</script>

<style scoped>

</style>